/*  File Name	: MakeMsgBlockTest.cpp
 *  Author		: Pang Jiacai
 *	 E-mail		: 823960168@qq.com
 *  Created on	: 2012-10-12
 *  Description:
 */

#ifndef MakeMsgBlockTest_CPP
#define MakeMsgBlockTest_CPP

#include "Logger.h"
#include "MakeMsgBlock.h"

void buffer_block_test(void)
{
	MakeMsgBlock buffer_block;
	buffer_block.init(100);

	MessageBlock in_mb(512);
	MessageBlock out_mb(512);

	while(true)
	{
		in_mb.reset();
		in_mb << 0;
		in_mb << 10000001;
		in_mb << 0;
		in_mb << std::string("javie");
		in_mb << std::string("test");
		in_mb << std::string("55555555555555555555555555555555555555555555555");
		char *p=in_mb.base_ptr();
		*p = in_mb.data_len();

		buffer_block.make_block(&in_mb,in_mb.data_len());

		out_mb.reset();
		int result = buffer_block.get_block(&out_mb);
		if (result < 0)
		{
			LOG((ERROR_LOG,"GateService::get_block error"));
		}
		else if (result == 0)
		{
			LOG((ERROR_LOG,"GateService::0"));
			sleep(3);
			return;
		}
		int cmd = 0,remain=0;
		std::string account="",pwd="",other="";
		out_mb >> cmd >>remain >>account >>pwd >>other;
		LOG((INFO_LOG,"%d-%d-%s-%s-%s",cmd,remain,account.c_str(),pwd.c_str(),other.c_str()));

		sleep(1);
	}
}

void make_block_test()
{
	MakeMsgBlock *block = new MakeMsgBlock(300);
	block->init();

	MessageBlock mb1(156);
	mb1 << (int)sizeof(int)*30;
	for(int i=0;i<30;i++)
	{
		mb1<< i;
	}

	int result = block->make_block(&mb1,mb1.data_len());
	LOG((DEBUG_LOG,"-result:[%d]-",result));


	MessageBlock mb2(156);
	mb2 << (int)sizeof(int)*40;
	for(int i=0;i<30;i++)
	{
		mb2<< i;
	}
	result = block->make_block(&mb2,mb2.data_len());
	LOG((DEBUG_LOG,"-result:[%d]-",result));

	result = block->get_block(&mb1);
	int out=0;
	for (int i = 0; i < 40; i++)
	{
		mb1 >> out;
		LOG((DEBUG_LOG,"-out:[%d][%d]-",i,out));
	}

	MessageBlock mb3(156);
	for(int i=0;i<10;i++)
	{
		mb3<< i;
	}
	 result = block->make_block(&mb3,mb3.data_len());
	LOG((DEBUG_LOG,"-result:[%d]-",result));

	if(result==0)
	{
		MessageBlock mb4(512);
		block->get_block(&mb4);
		int out=0;
		for(int i=0;i<40;i++)
		{
			mb4>>out;
			LOG((DEBUG_LOG,"-out:[%d][%d]-",i,out));
		}
	}
	delete block;
}

//int main(int argc,char *argv[])
//{
//	buffer_block_test();
//	return 0;
//}

#endif
